﻿#include<iostream>
using namespace std;
typedef long long LL;
const int N = 10010;
int n;
int q[N], temp[N];
LL merge_sort(int l, int r)
{
    if (l >= r)return 0;
    int mid = l + r >> 1;   //找一个中间值
    LL res = merge_sort(l, mid) + merge_sort(mid + 1, r);

    int k = 0;		 //用来记录temp数组中值的个数
    int i = l;		 //指向左边数组中的最小值
    int j = mid + 1; //指向右边数组中的最小值
    while (i <= mid && j <= r)
    {
        if (q[i] <= q[j])
            temp[k++] = q[i++];
        else
        {
            temp[k++] = q[j++];
            res += mid - i + 1;
        }
    }
    //比较完成后将其未比较的数继续放入temp数组中
    while (i <= mid)temp[k++] = q[i++];
    while (j < r)temp[k++] = q[j++];

    //将temp数组中的值，放回q数组中
    for (int i = l, j = 0; i <= r; i++, j++)
        q[i] = temp[j];
    return res;
}
int main()
{
    cout << "案例二" << endl;
    cin >> n;
    for (int i = 0; i < n; i++)cin >> q[i];
    
    cout << merge_sort(0, n - 1) << endl;
    
    system("pause");
    return 0;
};